package com.snifferx.util;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.faces.event.PhaseEvent;
import javax.faces.event.PhaseId;
import javax.faces.event.PhaseListener;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class LoginController implements PhaseListener  {

	private static final long serialVersionUID = 1L;
	protected final Log logger = LogFactory.getLog(getClass());


	public void afterPhase(PhaseEvent event) {

	}

	public void beforePhase(PhaseEvent event) {

		if (FacesContext.getCurrentInstance().getExternalContext()
				.getSessionMap().get("message") != null) {
			FacesContext.getCurrentInstance().addMessage(
					null,
					new FacesMessage(FacesMessage.SEVERITY_WARN,
							"You are logged in but You do not have proper rights to see this resource.",
							""));
			FacesContext.getCurrentInstance().getExternalContext()
					.getSessionMap().remove("message");
		}else if(FacesContext.getCurrentInstance().getExternalContext()
				.getSessionMap().get("DisabledException") != null){
			FacesContext.getCurrentInstance().addMessage(
					null,
					new FacesMessage(FacesMessage.SEVERITY_WARN,
							"Account is disabled, please contact administrator to deal with this issue.",
							""));
			FacesContext.getCurrentInstance().getExternalContext()
			.getSessionMap().remove("DisabledException");
		}

		
	}

	public PhaseId getPhaseId() {
		return PhaseId.RENDER_RESPONSE;
	}

	
}